package com.bysj.yqsht.service.impl;

import com.bysj.yqsht.bean.Classes;
import com.bysj.yqsht.bean.ClassesExample;
import com.bysj.yqsht.bean.extend.ClassesExtend;
import com.bysj.yqsht.dao.ClassesMapper;
import com.bysj.yqsht.dao.extend.ClassesExtendMapper;
import com.bysj.yqsht.service.IClassesService;
import com.bysj.yqsht.utils.CustomerException;
import com.bysj.yqsht.utils.DateUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class IClassesServiceImpl implements IClassesService {
    @Resource
    private ClassesMapper classesMapper;
    @Resource
    private ClassesExtendMapper classesExtendMapper;


    @Override
    public List<Classes> findAll() {
        ClassesExample example = new ClassesExample();
        return classesMapper.selectByExample(example);
    }

    @Override
    public void saveOrUpdate(Classes clas) {
        //转化为星期
        String week = DateUtils.dayForWeek(clas.getTimea());
        String time1 = clas.getTimea();
        String a = time1.substring(11);
        String time2 = clas.getTimeb();
        String b = time2.substring(11);
        clas.setTimea("每"+week+"的"+a);
        clas.setTimeb("每"+week+"的"+b);
        //搜索教师所有班级
        List<ClassesExtend> listClass = classesExtendMapper.findClassByTeacher(clas.getTeacherId());
        for (ClassesExtend ce: listClass) {
            String zhou1 = ce.getTimea();
            String zhou = zhou1.substring(1,4);
                  if(week.equals(zhou)){
                      String timea = ce.getTimea().substring(5);
                      String timeb = ce.getTimeb().substring(5);
                      if (a.compareTo(timea) > 0 && a.compareTo(timeb) < 0 ){
                          throw new CustomerException("所创建班级与已报班级时间冲突");
                      }else if (a.compareTo(timea) <0 && b.compareTo(timea) >0 ){
                          throw new CustomerException("所创建班级与已报班级时间冲突");
                      }
                  }
        }
        if(clas.getId() != null){
            classesMapper.updateByPrimaryKey(clas);
        }else {
           clas.setStudentes("0");
            classesMapper.insert(clas);
        }
    }

    @Override
    public void deleteById(long id) throws CustomerException {
        Classes classes = classesMapper.selectByPrimaryKey(id);
        if(classes == null){
            throw new CustomerException("删除失败，要删除的数据不存在");
        }
        classesMapper.deleteByPrimaryKey(id);

    }

    @Override
    public List<ClassesExtend> findAllWithTeacher(int pageNum, int limit){
        PageHelper.startPage(pageNum,limit);
        ClassesExample classExample = new ClassesExample();
        List<ClassesExtend> list1 = classesExtendMapper.selectWithTeacher();
        PageInfo<ClassesExtend> data = new PageInfo<ClassesExtend>(list1);
        List<ClassesExtend> list = data.getList();
        return list;
    }

    @Override
    public List<ClassesExtend> findAllWithSubject() {
        return classesExtendMapper.selectWithSubject();
    }

    @Override
    public List<ClassesExtend> findWithAllByTeacher(String teacher, Long teacherId, Long subjectId) {
        return classesExtendMapper.findWithAllByTeacher(teacher,teacherId,subjectId);
    }
}
